﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
namespace TDLib.Security
{
    /// <summary>
    /// 加密者类
    /// </summary>
    public class Encryptor
    {
        private EncryptTransformer transformer;
        private byte[] initVec;
        private byte[] encKey;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="algId"></param>
        public Encryptor(EncryptionAlgorithm algId)
        {
            transformer = new EncryptTransformer(algId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bytesData"></param>
        /// <param name="bytekey"></param>
        /// <param name="bytesIV"></param>
        /// <returns></returns>
        public byte[] Encrypt(byte[] bytesData, byte[] bytekey, byte[] bytesIV)
        {
            //设置流对象用来保存加密数据字节流. 
            using (MemoryStream memStreamEncryptedData = new MemoryStream())
            {
                transformer.IV = bytesIV;
                transformer.Key = bytekey;
                try
                {
                    using (ICryptoTransform transform = transformer.GetCryptoServiceProvider(bytekey))
                    {
                        using (CryptoStream encStream = new CryptoStream(memStreamEncryptedData, transform, CryptoStreamMode.Write))
                        {
                            //将加密数据写进流对象 
                            encStream.Write(bytesData, 0, bytesData.Length);
                            //设置加密的Key和初始向量IV属性 
                            encKey = transformer.Key;
                            initVec = transformer.IV;
                            encStream.FlushFinalBlock();
                            return memStreamEncryptedData.ToArray();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("在数据加密的时候出现错误！错误提示： \n" + ex.Message);
                }
                finally
                {
                    memStreamEncryptedData.Dispose();
                }
            }

        }

        /// <summary>
        /// 获取或设置加密的偏移向量
        /// </summary>
        public byte[] IV
        {
            get { return initVec; }
            set { initVec = value; }
        }

        /// <summary>
        /// 获取或设置加密的密钥
        /// </summary>
        public byte[] Key
        {
            get { return encKey; }
            set { encKey = value; }
        }

    }
}
